<template>
  <div class="common-layout">
    <el-container style="height:100%;width:100%;margin: 0 auto;">
      <!-- 头部样式 -->
      <el-header class="app-header">
        <div class="header-content">
          <p class="app-title">学生AI心理督导师</p>
        </div>
      </el-header>
      
      <!-- 主内容区域 - 左右布局 -->
      <el-main class="main-container">
        <div class="layout-wrapper">
          <!-- 左侧聊天区域 -->
          <div class="left-chat">
            <div id="chat" class="chat-container">
              <div class="chat-wrapper">
                <div v-for="(item, index) in form.msgList" :key="index" class="message-item">
                  <div class="user-question">
                    <div class="question-text">{{ item.question }}</div>
                  </div>
                  <div class="ai-answer">
                  <div class="answer-text">
                    <vue-markdown-render :source="item.answer" />
                  </div>
                </div>
                </div>
                
                <div v-if="form.isLoading" class="loading-indicator">
                  <el-icon class="is-loading"><Loading /></el-icon>
                  <span>AI正在思考中...</span>
                </div>
              </div>
            </div>
            
            <!-- 输入区域 -->
            <div class="input-area">
              <div class="input-container">
                <el-input 
                  v-model="form.input" 
                  @keyup.enter="sendMsg"
                  placeholder="请输入您的心理困惑或问题"
                  class="message-input"
                  :disabled="form.isLoading"
                >
                  <template #prefix>
                    <el-icon><ChatLineRound /></el-icon>
                  </template>
                </el-input>
                <el-button 
                  @click="sendMsg" 
                  class="send-button"
                  :disabled="!form.input.trim() || form.isLoading"
                  type="primary"
                >
                  发送
                  <el-icon class="icon-right"><Promotion /></el-icon>
                </el-button>
              </div>
              <div class="disclaimer">
                <p>
                  服务生成的所有内容均由人工智能模型生成，其生成内容的准确性和完整性无法保证，不代表我们的态度或观点
                </p>
              </div>
            </div>
          </div>
          
          <!-- 右侧相关案例和历史记录 -->
          <div class="right-sidebar">
            <!-- 相关案例 -->
            <div class="related-cases">
              <div class="section-title">相关案例</div>
              <div class="case-item">1. "战胜抑郁"：一个抑郁症患者的康复之路</div>
              <div class="case-item">2. "探索抑郁的深处"：一个心理治疗师的案例研究</div>
              <div class="case-item">3. "青少年抑郁症：家庭支持与心理治疗的成功故事"</div>
              <div class="case-item">4. "走出黑暗"：一个抑郁症康复者的自述</div>
            </div>
            
            <!-- 历史记录 -->
            <div class="history-section">
              <div class="section-title">历史记录</div>
              <div class="history-item">青少年厌学原因</div>
              <div class="history-item">抑郁症都有哪些</div>
            </div>
            <!-- neo4j关系图模拟 -->
            <div class="history-section">
              <neo4j/>
            </div>
          </div>
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script setup>
import { reactive, nextTick } from 'vue'
import { useAuthStore } from '@/stores/user'
import { ChatLineRound, Promotion, Loading } from '@element-plus/icons-vue'
import neo4j from './neo4j.vue'
import VueMarkdownRender from 'vue-markdown-render'
const authStore = useAuthStore()
const form = reactive({
  input: '',
  msgList: [],
  isLoading: false
})


async function sendMsg() {
  const keyword = form.input.trim()
  if (!keyword || form.isLoading) return

  try {
    form.isLoading = true
    
    form.msgList.push({
      question: keyword,
      answer: "AI生成中..."
    })
    form.input = ""
    setScrollToBottom()

    const response = await fetch('http://localhost:8080/api/ai/ask', {
        method: 'POST',
        headers: {
          'Authorization': `Bearer ${authStore.token}`,
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({
          question: keyword,
        })
    })

    if (!response.ok) {
      throw new Error(`请求失败: ${response.status}`)
    }

    const data = await response.json()
    
    form.msgList.pop()
    form.msgList.push({
      question: keyword,
      answer: data.answer || "未获取到有效回答"
    })

  } catch (error) {
    console.error("请求出错:", error)
    form.msgList.pop()
    form.msgList.push({
      question: keyword,
      answer: "请求失败，请稍后重试"
    })
  } finally {
    form.isLoading = false
    setScrollToBottom()
  }
}

async function setScrollToBottom() {
  await nextTick()
  const chatContainer = document.querySelector("#chat")
  if (chatContainer) {
    chatContainer.scrollTop = chatContainer.scrollHeight
  }
}
</script>

<style scoped>
/* 基础布局 */
.common-layout {
  height: 100%;
  background-color: #f5f7fa;
}

/* 头部样式 */
.app-header {
  height: 60px;
  padding: 0;
  background: #fff;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  border-bottom: 1px solid #eaeaea;
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}

.app-title {
  margin: 0;
  color: #333;
  font-size: 18px;
  font-weight: 600;
}

/* 主内容区域 */
.main-container {
  padding: 0;
  height: calc(100vh - 60px);
}

.layout-wrapper {
  display: flex;
  height: 100%;
}

/* 左侧聊天区域 */
.left-chat {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100%;
  border-right: 1px solid #eaeaea;
}

.chat-container {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  background-color: #fff;
}

.chat-wrapper {
  max-width: 800px;
  margin: 0 auto;
  width: 100%;
}

.message-item {
  margin-bottom: 20px;
}

.user-question {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 8px;
}

.question-text {
  background-color: #f0f7ff;
  padding: 12px 16px;
  border-radius: 18px;
  max-width: 80%;
  color: #333;
  font-size: 15px;
}

.ai-answer {
  display: flex;
  justify-content: flex-start;
}

.answer-text {
  background-color: #f9f9f9;
  padding: 12px 16px;
  border-radius: 18px;
  max-width: 80%;
  color: #333;
  font-size: 15px;
  line-height: 1.6;
}

.answer-text :deep(br) {
  content: "";
  display: block;
  margin-bottom: 10px;
}

/* 输入区域 */
.input-area {
  padding: 15px 20px;
  background-color: white;
  border-top: 1px solid #eaeaea;
}

.input-container {
  display: flex;
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
  gap: 10px;
  margin-bottom: 10px;
}

.message-input {
  flex: 1;
  border-radius: 20px;
}

.message-input :deep(.el-input__inner) {
  padding-left: 15px;
  border-radius: 20px;
  border: 1px solid #e0e4e8;
}

.message-input :deep(.el-input__prefix) {
  display: flex;
  align-items: center;
  padding-left: 10px;
  color: #6a11cb;
}

.send-button {
  border-radius: 20px;
  padding: 0 20px;
  background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
  border: none;
  color: white;
  transition: all 0.3s ease;
}

.send-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(106, 17, 203, 0.3);
}

.send-button:disabled {
  background: #c0c4cc;
  transform: none;
  box-shadow: none;
}

.icon-right {
  margin-left: 5px;
}

/* 免责声明 */
.disclaimer {
  max-width: 800px;
  margin: 0 auto;
  text-align: center;
}

.disclaimer p {
  margin: 0;
  color: #999;
  font-size: 11px;
  line-height: 1.4;
}

/* 右侧边栏 */
.right-sidebar {
  width: 300px;
  padding: 20px;
  background-color: #f9f9f9;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* 相关案例样式 */
.related-cases {
  padding: 15px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.section-title {
  font-weight: bold;
  margin-bottom: 10px;
  color: #333;
  font-size: 16px;
}

.case-item {
  padding: 8px 0;
  color: #666;
  font-size: 14px;
  border-bottom: 1px solid #eee;
}

.case-item:last-child {
  border-bottom: none;
}

/* 历史记录样式 */
.history-section {
  padding: 15px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.history-item {
  padding: 8px 0;
  color: #666;
  font-size: 14px;
  border-bottom: 1px solid #eee;
  cursor: pointer;
}

.history-item:last-child {
  border-bottom: none;
}

.history-item:hover {
  color: #6a11cb;
}

.loading-indicator {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px;
  color: #666;
  font-size: 14px;
}

.loading-indicator .el-icon {
  margin-right: 8px;
  animation: rotate 2s linear infinite;
}

@keyframes rotate {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

/* 响应式调整 */
@media (max-width: 992px) {
  .layout-wrapper {
    flex-direction: column;
  }
  
  .left-chat {
    border-right: none;
    border-bottom: 1px solid #eaeaea;
  }
  
  .right-sidebar {
    width: 100%;
    padding: 15px;
  }
  
  .chat-container {
    padding: 15px;
  }
  
  .input-area {
    padding: 10px 15px;
  }
  
  .question-text,
  .answer-text {
    max-width: 90%;
  }
}
.answer-text {
  background-color: #f9f9f9;
  padding: 12px 16px;
  border-radius: 18px;
  max-width: 80%;
  color: #333;
  font-size: 15px;
  line-height: 1.6;
  width: 100%;
}

/* Markdown内容样式 */
.answer-text :deep(h1) {
  font-size: 1.5em;
  margin: 0.8em 0;
  font-weight: bold;
}

.answer-text :deep(h2) {
  font-size: 1.3em;
  margin: 0.7em 0;
  font-weight: bold;
}

.answer-text :deep(h3) {
  font-size: 1.1em;
  margin: 0.6em 0;
  font-weight: bold;
}

.answer-text :deep(p) {
  margin: 0.5em 0;
}

.answer-text :deep(ul),
.answer-text :deep(ol) {
  margin: 0.5em 0;
  padding-left: 1.5em;
}

.answer-text :deep(li) {
  margin: 0.3em 0;
}

.answer-text :deep(blockquote) {
  border-left: 3px solid #6a11cb;
  padding-left: 1em;
  margin: 1em 0;
  color: #666;
}

.answer-text :deep(code) {
  background-color: #f0f0f0;
  padding: 0.2em 0.4em;
  border-radius: 3px;
  font-family: monospace;
}

.answer-text :deep(pre) {
  background-color: #f5f5f5;
  padding: 1em;
  border-radius: 5px;
  overflow-x: auto;
  margin: 1em 0;
}

.answer-text :deep(pre code) {
  background-color: transparent;
  padding: 0;
}

.answer-text :deep(a) {
  color: #2575fc;
  text-decoration: none;
}

.answer-text :deep(a:hover) {
  text-decoration: underline;
}

.answer-text :deep(strong) {
  font-weight: bold;
}

.answer-text :deep(em) {
  font-style: italic;
}

.answer-text :deep(table) {
  border-collapse: collapse;
  width: 100%;
  margin: 1em 0;
}

.answer-text :deep(th),
.answer-text :deep(td) {
  border: 1px solid #ddd;
  padding: 0.5em;
}

.answer-text :deep(th) {
  background-color: #f2f2f2;
}
</style>